using System;
using System.Collections.Generic;
using System.Text;

namespace GameFramework
{
    public abstract class GameEntity
    {
        protected List<Entity> entities = null;
        protected List<Action> actions = null;
        protected List<Form> forms = null;
        private bool running = true;
        private string returnID = null;
        
        public GameEntity()
        {
            entities = new List<Entity>();
            actions = new List<Action>();
            forms = new List<Form>();
        }

        public abstract void Init();

        public string Run()
        {
            while (running)
            {
                for (int i = 0; i < actions.Count; i++)
                    actions[i].Run();
                GameManager.GetServices().display.Begin();
                for (int i = 0; i < forms.Count; i++)
                    forms[i].GetMesh().Render();
                GameManager.GetServices().display.End();
                GameManager.GetServices().input.Refresh();
                Update();
            }

            return returnID;
        }
        
        public abstract void Update();

        protected void Terminate()
        {
            for (int i = 0; i < entities.Count; i++)
                entities[i].Terminate();
            running = false;
        }

        public void AddForm(Form form)
        {
            if(!forms.Contains(form))
                forms.Add(form);
        }
        
        public void RemoveForm(Form form)
        {
            if (forms.Contains(form))
                forms.Remove(form);
        }

        public void ActivateAction(Action action, bool value)
        {
            if (value)
            {
                if (!actions.Contains(action))
                    actions.Add(action);
            }
            else
                if (actions.Contains(action))
                    actions.Remove(action);
                
        }
        
        public void RemoveEntity(Entity entity)
        {
            entities.Remove(entity);
        }

        public List<Entity> GetEntities()
        {
            return entities;
        }

        protected void TerminateAndLaunch(string ID)
        {
            returnID = ID;
            Terminate();
        }

    }
}
